//
//  ViewController.m
//  2048
//
//  Created by Justsoso on 16/3/19.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "ViewController.h"

#import "CellView.h"

@interface ViewController ()

@property (nonatomic,strong) UIView *boxView;

@property (nonatomic,strong) NSMutableArray *boxArr;

@property (nonatomic,strong) NSMutableArray *mergeCellIdArr;

@property (nonatomic,strong) NSMutableArray *needRemoveViewArr;



@end

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

#define W(x) ((x) * kScreenWidth / 375.0)
#define H(y) ((y) * kScreenHeight / 667.0)


@implementation ViewController

- (NSMutableArray *)boxArr
{
    if (_boxArr == nil)
    {
        _boxArr = [NSMutableArray array];
        for (int i=0; i<N*N; i++)
        {
            [_boxArr addObject:@0];
        }
    }
    return _boxArr;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.boxView.backgroundColor = kRGBBackgroundColor;
    
    self.mergeCellIdArr = [NSMutableArray array];
    self.needRemoveViewArr = [NSMutableArray array];
    
    [self drawBackBox];
    [self drawBackCell];
    
    
//    UISwipeGestureRecognizerDirectionRight 
//    UISwipeGestureRecognizerDirectionLeft  
//    UISwipeGestureRecognizerDirectionUp
//    UISwipeGestureRecognizerDirectionDown
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)];
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)];
    UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)];
    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:swipeLeft];
    [self.view addGestureRecognizer:swipeRight];
    [self.view addGestureRecognizer:swipeUp];
    [self.view addGestureRecognizer:swipeDown];
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self addNewCell];
}

#pragma mark - 1. 绘制底板
- (void)drawBackBox
{
    CGFloat boxWidth = W(kCellWidth) * N + W(kCellPadding) * N * 2 + W(kBoxPadding) * 2;
    
    self.boxView = [[UIView alloc] init];
    self.boxView.bounds = CGRectMake(0, 0, boxWidth, boxWidth);
    self.boxView.center = CGPointMake(kScreenWidth/2, kScreenHeight/2+80);
    self.boxView.backgroundColor = kRGBoxColor;
    self.boxView.layer.cornerRadius = kBoxCornerRadius;
    [self.view addSubview:self.boxView];
}

- (void)drawBackCell
{
    for (int i=0; i<N; i++)
    {
        for (int j=0; j<N; j++)
        {
            CGFloat cellX = W(kCellWidth) * j + W(kCellPadding) * j * 2 + W(kCellPadding) + W(kBoxPadding);
            CGFloat cellY = H(kCellWidth) * i + H(kCellPadding) * i * 2 + H(kCellPadding) + H(kBoxPadding);
            
            UIView *cellView = [[UIView alloc] initWithFrame:CGRectMake(cellX, cellY, W(kCellWidth), W(kCellWidth))];
            cellView.backgroundColor = kRGBCellColor;
            cellView.layer.cornerRadius = kCellConrerRadius;
            [self.boxView addSubview:cellView];
        }
    }
}

#pragma mark - 2. 产生新格子
- (void)addNewCell
{
    // 1. 新格子产生位置
    int cellId = [self getEmptyCellId];
    if (cellId == -1)
    {
        return;
    }
    // 2. 产生一个数字
    int number = [self getNumber];
    // 3. 在该位置创建格子
    
    //  3.1 数据放到self.boxArr中保存
    self.boxArr[cellId] = @(number);
    
    //  3.2 添加对应视图到界面
    
    [self createCellByCellId:cellId andNumber:number];
    
}

- (void)createCellByCellId:(int)cellId andNumber:(int)number
{
    CellView *cellView = [[CellView alloc] init];
    
    int row = cellId / N;
    int col = cellId % N;

    CGFloat cellX = W(kCellWidth) * col + W(kCellPadding) * col * 2 + W(kCellPadding) + W(kBoxPadding);
    CGFloat cellY = H(kCellWidth) * row + H(kCellPadding) * row * 2 + H(kCellPadding) + H(kBoxPadding);
    cellView.frame = CGRectMake(cellX, cellY, W(kCellWidth), W(kCellWidth));
    
    cellView.number = number;
    [self.boxView addSubview:cellView];
    
    cellView.transform = CGAffineTransformMakeScale(0.3, 0.3);
    [UIView animateWithDuration:0.1
                          delay:0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         cellView.transform = CGAffineTransformMakeScale(1.2, 1.2);
                   } completion:^(BOOL finished) {
                         cellView.transform = CGAffineTransformMakeScale(1, 1);
                   }];

}
- (int)getEmptyCellId
{
    NSMutableArray *tmpArr = [NSMutableArray array];
    for (int i=0; i<N*N; i++)
    {
        // 1. 将所有空格子的编号，加入到一个数组
        if ([self.boxArr[i] intValue] == 0)// 空格子
        {
            [tmpArr addObject:@(i)];
        }
    }
    if (tmpArr.count < 1) // 没有空格子
    {
        return -1;
    }
    
    // 2. 从这个数组中随机选一个
    int rand = arc4random_uniform((int)tmpArr.count);
    
    // 3. 格子的编号
    int cellId = [tmpArr[rand] intValue];
    
    return cellId;
}

- (int)getNumber
{
    if (arc4random_uniform(10) == 0)
    {
        return 4;
    }
    else
    {
        return 2;
    }
}



#pragma mark - 移动

- (void)swip:(UISwipeGestureRecognizer *)g
{
//    UISwipeGestureRecognizerDirectionRight
//    UISwipeGestureRecognizerDirectionLeft
//    UISwipeGestureRecognizerDirectionUp
//    UISwipeGestureRecognizerDirectionDown
//    switch (g.direction)
//    {
//        case UISwipeGestureRecognizerDirectionLeft:
//        {
//            for (<#initialization#>; <#condition#>; <#increment#>) {
//                <#statements#>
//            }
//        }
//            break;
//            
//        default:
//            break;
//    }
}
@end


























